<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once('includes/functions/module_functions.php');
class ot_special {

	function ot_special() {
		$this->module_key = 'MODULE_ORDER_TOTAL_SPECIAL';
		$this->code = 'ot_special';
		$this->enabled = ((MODULE_ORDER_TOTAL_SPECIAL_STATUS == 'true') ? true : false);
		$this->sort_order = MODULE_ORDER_TOTAL_SPECIAL_SORT_ORDER;
	}

	function process(&$params, &$checkout_component)
	{
		$accountId = (int)$params['accountId'];

		$cart = &$checkout_component->cart;
		$cart_prices = &$checkout_component->cart_prices;

		$subtotal = $cart_prices['ot_subtotal']['value'];
		$tax = $cart_prices['ot_tax']['value'];

		//Special
		$special_discount = $subtotal * ((double)MODULE_ORDER_TOTAL_SPECIAL_PERCENTAGE_OFF / 100) * -1.0;
		$limit_min = (double)MODULE_ORDER_TOTAL_SPECIAL_MIN_LIMIT * -1.0;
		$limit_max = (double)MODULE_ORDER_TOTAL_SPECIAL_MAX_LIMIT * -1.0;
		if ($special_discount > $limit_min) $special_discount = $limit_min;
		else if ($special_discount < $limit_max) $special_discount = $limit_max;
		
		$cart_prices[$this->code] = array('value' => $special_discount, 'title' => 'Internet Special:', 'class' => $this->code);

		$cartTax = 0.0;
		$sz = sizeof($cart);
		for ($i=0; $i< $sz; $i++)
		{
			$cartTax += ((double)$cart[$i]['tax'] * (int)$cart[$i]['cart_qty']);
		}
		$tax_discount = 0.0;
		if ($cartTax > 0)
		{	
			$tax_percentage = $cartTax/$subtotal;
			$tax_discount = $coupon_discount * $tax_percentage;
			$cart_prices['ot_tax']['value'] += $tax_discount;
		}

	}

	function check() {
		if (!isset($this->_check)) {
			$this->_check = check_module($this->module_key);
		}
		return $this->_check;
	}

	function install() {
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, configuration_values, date_added) values ('Display Internet Special', 'MODULE_ORDER_TOTAL_SPECIAL_STATUS', 'false', 'Do you want to display the special discount?', '6', '1','true, false', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_SPECIAL_SORT_ORDER', '5', 'Sort order of display.', '6', '2', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Percentage Off', 'MODULE_ORDER_TOTAL_SPECIAL_PERCENTAGE_OFF', '2', 'Percentage.', '6', '2', now())");
		run_sql("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum Amount Off', 'MODULE_ORDER_TOTAL_SPECIAL_LIMIT', '50', 'Amount off will not exceed this value.', '6', '2', now())");
	}

	function remove() {
		remove_module($this->module_key);
	}
}
?>
